<%@ page import="dao.CartsDAO" %>
<%@ page import="entity.Carts" %>
<%@ page import="entity.Product" %>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>购物车</title>
    <link href="//cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
        <%
            CartsDAO cartsDAO = new CartsDAO();
            List<Carts> cartsList = cartsDAO.queryAllCarts();

            if ( cartsList == null || cartsList.size() <= 0 ) {
        %>

        <div class="jumbotron">
            <h1>您的购物车为空！</h1>
            <p>快去<a class="btn btn-danger btn-lg" href="index.jsp" role="button" > 血 拼 </a>吧！！！</p>
        </div>

        <%
                return;
            }
        %>
        <table class="table table-bordered table-condensed table-hover table-striped">
            <tr>
                <th>商品编号</th>
                <th>商品名称</th>
                <th>商品单价</th>
                <th>商品库存</th>
                <th>操作</th>
            </tr>

        <%
            for (Carts c : cartsList) {
        %>
        <tr>
            <td><%=c.getId()%></td>
            <td><%=c.getProduct().getName()%></td>
            <td><%=c.getProduct().getPrice()%></td>
            <td><%=c.getCount()%></td>
            <td>
                <a href="decarts.jsp?cid=<%=c.getId()%>&pid=<%=c.getProduct().getId()%>&count=<%=c.getCount()%>" class="btn btn-danger"><b class="glyphicon glyphicon-trash"></b>删除</a>
                <input type="number" id="carts<%=c.getId()%>" style="width:70px;" value="<%=c.getCount()%>">
                <a href="javascript:modify(<%=c.getId()%>,<%=c.getCount()%>,<%=c.getProduct().getId()%>)" class="btn btn-success"><i class="glyphicon glyphicon-pencil"></i>修改</a>
            </td>
        </tr>
        <%
            }
        %>
    </table>
</div>

<script type="text/javascript">
    function modify(id, oldCount, pid){
        var e_text = document.getElementById("carts" + id);
        var count = parseInt(e_text.value);
        if ( count > 1 ) {
            window.location.href="mocarts.jsp?cid=" + id + "&count=" + count + "&ocount=" + oldCount + "&pid=" + pid;
        }
    }
</script>
</body>
</html>
